package whu.cn.whushare.ui.main.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.dd.processbutton.iml.ActionProcessButton;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.common.Constants;
import whu.cn.whushare.common.UserManager;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.common.LoadingHelper;
import whu.cn.whushare.ui.common.MainReShowFragment;
import whu.cn.whushare.ui.main.MainTabActivity;
import whu.cn.whushare.ui.main.activity.ActivityDetailActivity;
import whu.cn.whushare.ui.main.activity.bean.Activity;
import whu.cn.whushare.ui.main.known.AddAnswerActivity;
import whu.cn.whushare.ui.main.known.AnswerDetailActivity;
import whu.cn.whushare.ui.main.known.InviteAnswerActivity;
import whu.cn.whushare.ui.main.known.QuestionDetailActivity;
import whu.cn.whushare.ui.main.known.QuestionListActivity;
import whu.cn.whushare.ui.main.known.adapter.AllQuestionListAdapter;
import whu.cn.whushare.ui.main.known.adapter.AnswerListAdapter;
import whu.cn.whushare.ui.main.known.adapter.TopicAdapter;
import whu.cn.whushare.ui.main.known.bean.AllQuestion;
import whu.cn.whushare.ui.main.known.bean.AnswerWrapBean;
import whu.cn.whushare.ui.main.known.bean.FavorBean;
import whu.cn.whushare.ui.main.known.bean.Question;
import whu.cn.whushare.ui.main.known.bean.QuestionDetail;
import whu.cn.whushare.ui.main.known.bean.Topic;
import whu.cn.whushare.ui.main.my.adapter.AllTopicAdapter;
import whu.cn.whushare.ui.main.my.adapter.MyActivityListAdapter;
import whu.cn.whushare.ui.main.my.adapter.MyDynamicListAdapter;
import whu.cn.whushare.ui.main.my.bean.AllTopic;
import whu.cn.whushare.ui.main.my.bean.MyActivity;
import whu.cn.whushare.ui.main.my.bean.MyDynamic;
import whu.cn.whushare.util.GUIDGenerator;
import whu.cn.whushare.util.SetOnClickUtil;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2015/4/11.
 */
@ViewMapping(id=R.layout.activity_my_dynamic)
public class MyDynamicActivity extends BaseActivity {
    @ViewMapping(id = R.id.my_dynamic_list)
    private PullToRefreshListView mRefreshListView;
    private LoadingHelper loadingHelper;
    private boolean mHasInited;
    private MyDynamicListAdapter adapter;
    private List<MyDynamic> myDynamics;
    private int mCurPage = 1;
    private int pageSize=20000;

    private Question mQuestionId;
    public static Intent getStartIntent(Context context) {
        Intent i = new Intent(context, MyDynamicActivity.class);
        return i;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        initParams();
        initViews();
        initActions();

    }
    private void initLoadingHelper(){
        loadingHelper = new LoadingHelper(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestMyDynamicList(mCurPage,pageSize,GUIDGenerator.getGUID());
            }
        });
        loadingHelper.onCreateView(LayoutInflater.from(this), mRefreshListView);
        requestMyDynamicList(mCurPage, pageSize, GUIDGenerator.getGUID());
    }
    private void initParams() {

    }

    private void requestMyDynamicList(int page,int pageSize,String dynamicId) {
        loadingHelper.showLoadingView(mHasInited);
        Downloader.getInstance(this).getMyDynamicList(page ,pageSize,dynamicId,this,page);
    }

    private void initViews() {
        View root = ViewMappingUtil.mapView(this, this);
        SetOnClickUtil.registerOnClick(this);
        getTopBar(root);
        mTopBar.title.setText("最新动态");
        initLoadingHelper();
        mRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
          ;
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {

                requestMyDynamicList(mCurPage,pageSize, GUIDGenerator.getGUID());
               Utils.resetRefreshLabel(MyDynamicActivity.this, mRefreshListView);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                if (myDynamics.size()>=mTotal) {
                    Utils.setRefreshLabelToLast(MyDynamicActivity.this, mRefreshListView);
                    Utils.COMMON_HANDLER.sendMessageDelayed(
                            Message.obtain(Utils.COMMON_HANDLER, Utils.REFRESH_TO_COMPLETE, mRefreshListView), 500);
                } else {
                    requestMyDynamicList(mCurPage+1,pageSize, GUIDGenerator.getGUID());
                }
            }
        });
    }

    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);
        mCurPage = (int) obj;
        switch (type) {
            case MY_DYNAMIC:
                List<MyDynamic> dynamics=(List<MyDynamic>)response;

                if (myDynamics == null || mCurPage == 1) {
                   myDynamics = dynamics;

                    adapter = new MyDynamicListAdapter(this,myDynamics);
                    mRefreshListView.setAdapter(adapter);
                } else {

                    myDynamics.addAll(dynamics);
                    adapter.notifyDataSetChanged();
                }


                mHasInited = true;
                loadingHelper.showContentView();
                mRefreshListView.onRefreshComplete();

                break;
            default:
                break;
        }
    }

    @Override
    public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
        super.onFail(type, error);
        if (!mHasInited) {
            loadingHelper.showRetryView();
        }
    }
    private void initActions() {
        mRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                MyDynamic myDynamic= (MyDynamic) adapter.getItem(arg2 - 1);
                String   mQuestionId = myDynamic.sourceQuestion.getId();
                startActivityForResult(QuestionDetailActivity.getStartIntent(MyDynamicActivity.this, mQuestionId), 1);
            }

        });

    }

}